﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="TargetColor.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Modifier
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT.ParticleSystem.Common;
    #endregion

    /// <summary>
    /// The TargetColor modifier fades the color of a particle towards a target color. On 
    /// every update the color of the particle moves a little closer to the target color. The 
    /// rate at which particles approach this color is controlled by the rate property.
    /// </summary>
    public class TargetColor : ModifierBase
    {
        #region Member Variables
        private Color _color;
        private float _rate;

        #endregion

        #region Constructors
        /// <summary>
        /// The constructor creates a TargetColor modifier to be used by an emitter.
        /// </summary>
        public TargetColor()
            : this(new Color(255, 255, 255, 255), 0.1f)
        {
        }

        /// <summary>
        /// The constructor creates a TargetColor modifier to be used by an emitter.
        /// </summary>
        /// <param name="color">
        /// The target color that should be reached
        /// </param>
        /// <param name="rate">
        /// Adjusts how quickly the particle approaches the target color. Larger numbers 
        /// cause a higher speed.
        /// </param>
        public TargetColor(Color color, float rate)
        {
            this._color = color;
            _rate = rate;
        }

        #endregion

        /// <summary>
        /// The target color that should be reached
        /// </summary>
        public Color Color
        {
            get { return this._color; }
            set { this._color = value; }
        }

        /// <summary>
        /// Adjusts how quickly the particle approaches the target color. Larger numbers 
        /// cause a higher speed.
        /// </summary>
        public float Rate
        {
            get { return _rate; }
            set { _rate = value; }
        }

        /// <summary>
        /// Moves the actual color a little closer to the target color taking the rate and the 
        /// time since last update into account.
        /// <p>This method is called by the emitter and need not be called by the user</p>
        /// </summary>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to be updated.</param>
        /// <param name="time">The duration of the frame - used for time based updates.</param>
        public override void Update(Emitter emitter, Particle particle, float time)
        {
            float inv = MathHelper.Clamp(_rate * time, 0.0f, 1.0f);
            float ratio = 1.0f - inv;

            particle.color.R = (byte)(particle.color.R * ratio + this._color.R * inv);
            particle.color.G = (byte)(particle.color.G * ratio + this._color.G * inv);
            particle.color.B = (byte)(particle.color.B * ratio + this._color.B * inv);
            particle.color.A = (byte)(particle.color.A * ratio + this._color.A * inv);
        }
    }
}
